package com.vrv.app.cyberspace.modules.sys.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.vrv.app.cyberspace.common.utils.HttpContextUtils;
import com.vrv.app.cyberspace.common.utils.JSONResult;
import com.vrv.app.cyberspace.modules.sys.entity.*;
import com.vrv.app.cyberspace.modules.sys.mapper.BaseTableMapper;
import com.vrv.app.cyberspace.modules.sys.mapper.InfrastructureInformationTableMapper;
import com.vrv.app.cyberspace.modules.sys.mapper.RiskTableMapper;
import com.vrv.app.cyberspace.modules.sys.service.IRiskTableService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.vrv.app.cyberspace.common.utils.JSONResult.renderError;
import static com.vrv.app.cyberspace.common.utils.JSONResult.renderSuccess;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sm
 * @since 2017-12-15
 */
@SuppressWarnings({"Duplicates", "SpringJavaInjectionPointsAutowiringInspection"})
@Service
@Transactional
public class RiskTableServiceImpl extends ServiceImpl<RiskTableMapper, RiskTable> implements IRiskTableService {
    Logger logger = LoggerFactory.getLogger(RiskTableServiceImpl.class);

    @Autowired
    private BaseTableMapper baseTableMapper;
    @Autowired
    private RiskTableMapper riskTableMapper;
    @Autowired
    private InfrastructureInformationTableMapper infrastructureInformationTableMapper;

    @Override
    public JSONResult addRisk(BaseTable bt, RiskTable rt, String infrastructure_information) {
        UserTable userTable = (UserTable) HttpContextUtils.getHttpServletRequest().getSession().getAttribute("user");
        Date now = new Date();
        if (StringUtils.isEmpty(bt.getCreateUserID())) {
            return renderError("createUserID参数不能为空！");
        }
        bt.setInfoType(2);
        bt.setCreateTime(now);
        bt.setUpdateTime(now);
        baseTableMapper.insert(bt);
        rt.setBaseID(bt.getId());
        if (StringUtils.isNotEmpty(rt.getEnclosure())) {
            List<Integer> attIDs = new ArrayList<>();
            try {
                JSONArray upLoadFiles = JSONArray.parseArray(rt.getEnclosure());
                for (Object upLoadFile :
                        upLoadFiles) {
                    AttachmentTable attachmentTable = new AttachmentTable();
                    attachmentTable.setName(upLoadFile.toString().substring(upLoadFile.toString().lastIndexOf("/") + 1));
                    attachmentTable.setUrl(upLoadFile.toString());
                    attachmentTable.insert();
                    attIDs.add(attachmentTable.getId());
                }
            } catch (Exception e) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return renderError("附件参数格式异常！");
            }
            rt.setEnclosure(StringUtils.join(attIDs, ","));
        }
        List<InfrastructureInformationTable> iits = JSONArray.parseArray(infrastructure_information, InfrastructureInformationTable.class);
        if (StringUtils.isNotEmpty(infrastructure_information)) {
            List<Integer> infrastructureIDs = new ArrayList<>();
            for (InfrastructureInformationTable iit :
                    iits) {
                infrastructureInformationTableMapper.insert(iit);
                infrastructureIDs.add(iit.getId());
            }
            rt.setInfrastructureIDs(StringUtils.join(infrastructureIDs, ","));
        }
        riskTableMapper.insert(rt);
        logger.info("新增风险" + bt.getId() + "由" + userTable.getLinkdoodID() + "-" + userTable.getLinkdoodName() + "新增");
        return renderSuccess();
    }

    @Override
    public JSONResult updateRisk(Integer id, BaseTable bt, RiskTable rt) {
        UserTable userTable = (UserTable) HttpContextUtils.getHttpServletRequest().getSession().getAttribute("user");
        bt.setId(id);
        bt.setUpdateTime(new Date());
        baseTableMapper.updateById(bt);
        rt.setBaseID(id);
        riskTableMapper.updateById(rt);
        logger.info("变更风险" + bt.getId() + "由" + userTable.getLinkdoodID() + "-" + userTable.getLinkdoodName() + "变更");
        return renderSuccess();
    }
}
